#pragma once
#include <iostream>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "Log.hpp"

class Util
{
public:
    //获取网页文件大小并读取网页文件
    static bool ReadFile(const std::string &path, std::string &fileContent)
    {
        struct stat st;
        int n = stat(path.c_str(), &st);
        if (n < 0) return false;
        int size = st.st_size;//文件的长度

        fileContent.resize(size);
        int fd=open(path.c_str(),O_RDONLY);
        if(fd<0) return false;
        read(fd,(char*)fileContent.c_str(),size);
        logMessage(Info,"read file:%s done",path.c_str());
        close(fd);
        return true;
    }

    //提取request的第一行
    static void ReadOneLine(std::string&message,std::string&line,const std::string&sep)
    {
        auto pos=message.find(sep);
        if(pos==std::string::npos) return;
        line=message.substr(0,pos);
        message.erase(0,pos+sep.size());
    }

    //解析request的第一行
    static bool ParseLine(const std::string&line,std::string&method,std::string&url,std::string&version)
    {
        std::stringstream ss(line);
        ss>>method>>url>>version;
        return true;
    }
};